package msu.erdc.hci;

import java.util.ArrayList;

import msu.erdc.hci.customObjects.ContactObject;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactsAdapter extends BaseAdapter{

    Context context; 
    int layoutResourceId;    
    ArrayList <ContactObject> contactObj;
    ContactListener callback;
    
    public interface ContactListener
    {
    	public void name_click(int index);
    	public void email_click(ContactObject c);
    	public void sms_click(ContactObject c);
    	public void call_click(ContactObject c);
    	
    };
   
    
    public ContactsAdapter(Context context, int layoutResourceId, ArrayList <ContactObject> data, ContactListener listener) {
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        contactObj = data;
        callback=listener;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ContactsHolder holder = null;
        
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            
            holder = new ContactsHolder();
            holder.txtTitle = (TextView)row.findViewById(R.id.nameText);
            holder.imgv=(ImageView)row.findViewById(R.id.callButton);
            holder.text=(ImageView)row.findViewById(R.id.textButton);
            holder.email=(ImageView)row.findViewById(R.id.emailButton);
            row.setTag(holder);
        }
        else
        {
            holder = (ContactsHolder)row.getTag();
        }
        
        final ContactObject c = contactObj.get(position);
        holder.txtTitle.setText(c.getName());
        
        holder.txtTitle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				callback.name_click(position);
			}});
       
        holder.imgv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				
				callback.call_click(c);
			    
			}});
        
        
      
            holder.text.setOnClickListener(new OnClickListener() {

    			@Override
    			public void onClick(View arg0) {
    				
    				//
					//FragmentTransaction ft = context.getFragmentManager().beginTransaction();
					//ft.replace(android.R.id.content, new SendSMS(), "SMS");
					//ft.addToBackStack(null);
					//ft.commit();
    				callback.sms_click(c);
    			}});
            
        
        
            holder.email.setOnClickListener(new OnClickListener() {

    			@Override
    			public void onClick(View arg0) {
    				callback.email_click(c);
    				
    			}});
     
        
        
        return row;
    
        
    }
   
    
    
    @Override
    public int getCount()
    {
    	return contactObj.size();
    }
    
    @Override
    public Object getItem(int position)
    {
    	return contactObj.get(position);
    }
    
    @Override
    public long getItemId(int position)
    {
    	return position;
    }
    
    
    static class ContactsHolder
    {
    	ImageView imgv;
        TextView txtTitle;
        ImageView text;
        ImageView email;
        
    }
    
    private void call(){
    	/*
        try {

       Intent callIntent = new Intent(Intent.ACTION_CALL);
       callIntent.setData(Uri.parse("tel:123456789"));
       startActivity(callIntent);
       } catch (ActivityNotFoundException e) {
       Log.e("helloandroid dialing example", "Call failed", e);
       }*/
    }

    
}
